package com.example.server.web.utils;


import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

/**
 * @description uuid生成器
 */
public class UUIDKeyGenerator {
    public static String generate(String prefix) {
        String uuid = UUID.randomUUID().toString();
        return prefix + uuid.replaceAll("-", "");
    }

    /**
     * 生成四位字符的uuid
     * @param isUpperCase 是否需要大写
     * @return
     */
    public static String generate4UUID(Boolean isUpperCase){
        UUID id=UUID.randomUUID();
        String[] uuid=id.toString().split("-");
        if (isUpperCase){
            return uuid[1].toUpperCase();
        }
        return uuid[1];
    }

    public static String generateCode(String prefix,Boolean isUpperCase){
        String uuid = generate4UUID(true);
        Date nowDate = DateUtils.getNowDate();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = format.format(nowDate);
        String[] dateAarry = dateString.split("-");
        //获取天数
        return prefix+dateAarry[2]+uuid;
    }
}
